package com.ecreditpal.audio.service;

import com.alibaba.fastjson.JSONObject;
import com.ecreditpal.audio.model.AudioSession;
import com.ecreditpal.audio.util.MyHttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lifeng
 * @version 1.0 on 2018/7/19.
 */
@Service
@Slf4j
public class SeechRecordService {

    public boolean recordToRemote(AudioSession session) {
        JSONObject entity = new JSONObject();

        JSONObject local = new JSONObject();
        JSONObject remote = new JSONObject();

        entity.put("local", local);
        entity.put("remote", remote);

        local.put("caseId", session.getCaseId());
        local.put("callId", session.getCallId());
        local.put("records", session.getLocal());

        remote.put("caseId", session.getCaseId());
        remote.put("callId", session.getCallId());
        remote.put("records", session.getRemote());


        Map<String,String> params = new HashMap<>();
        params.put("recordjson",entity.toJSONString());
        String result = MyHttpClient.post("http://www.yubang168.cn/recordAdd.aspx", params);

        JSONObject resp = JSONObject.parseObject(result);
        Integer code = resp.getInteger("code");

        String message;
        switch (code) {
            case 1:
                message = "添加成功";
                break;
            case 0:
                message = "添加失败";
                break;
            case -1:
                message = "必填参数为空";
                break;
            case -2:
                message = "必填参数中(audioId)为空";
                break;
            default:
                message = "添加失败";
        }
        log.info("speech record remote add result: {},code:{}", message, code);

        return code == 1;
    }

    public static void main(String[] args) {
        AudioSession session = new AudioSession();
        session.setCallId("666");
        session.setCaseId("777");


        JSONObject entity = new JSONObject();
        JSONObject body = new JSONObject();

        JSONObject local = new JSONObject();
        JSONObject remote = new JSONObject();

        entity.put("local", local);
        entity.put("remote", remote);

        local.put("caseId", session.getCaseId());
        local.put("callId", session.getCallId());
        local.put("records", session.getLocal());

        remote.put("caseId", session.getCaseId());
        remote.put("callId", session.getCallId());
        remote.put("records", session.getRemote());

        body.put("recordjson",entity);
        System.out.println(body.toJSONString());

        while (true) {
            System.out.println(1);

            String result = MyHttpClient.post("http://www.yubang168.cn/recordAdd.aspx", null);
        }
    }
}
